/**
 *    Copyright (C) 2011 Nadim Benabdenbi <nadim.benabdenbi@gmail.com>
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.google.code.maven.plugin.beans.builder;

import org.xml.sax.ErrorHandler;

import com.sun.codemodel.JClassAlreadyExistsException;
import com.sun.codemodel.JDefinedClass;
import com.sun.tools.xjc.Options;
import com.sun.tools.xjc.Plugin;
import com.sun.tools.xjc.outline.ClassOutline;
import com.sun.tools.xjc.outline.Outline;

/**
 * 
 */
public class XjcBeansBuilderPatternPlugin extends Plugin {
	
	@Override
	public String getOptionName() {
		return "Xbuilder-api";
	}
	
	@Override
	public String getUsage() {
		return "  -Xbuilder-api        :  generates builders for every generated bean";
	}
	
	@Override
	public boolean run(Outline outline, Options opt, ErrorHandler errorHandler) {
		// Process every pojo class generated by jaxb
		for (ClassOutline classOutline : outline.getClasses()) {
			final JDefinedClass targetImplClass = classOutline.implClass;
			try {
				new CodeModelBuilderSourceGenerator(outline.getCodeModel(), targetImplClass).generate();
			} catch (JClassAlreadyExistsException e) {
				throw new RuntimeException("builder generation cylce failure", e);
			}
		}
		return true;
	}
}
